<?php
class LinesModel extends Model {
	protected $tableName = "lines";
	public function getLinesList($map = '', $show_inspector = true, $field = '*', $order = 'line_id ASC', $limit = 30) {
		$res = $this->where ( $map )->field ( $field )->order ( $order )->limit ( $limit )->select ();
		
		$inspector_id = $this->getSubByKey ( $res, 'inspector_id' );
		
		if ($show_inspector) {
			$map ['inspector_id'] = array (
					'in',
					$inspector_id 
			);
			$temp_inspector = M ( 'inspector' )->where ( $map )->order ( 'inspector_id ASC' )->select ();
			
			$inspector = array ();
			
			foreach ( $temp_inspector as $v ) {
				$inspector [$v ['inspector_id']] = $v ['name'];
			}
			unset ( $temp_inspector );
			
			// 将用户组信息添加至结果集
			
			foreach ( $res as $k => $v ) {
				$res [$k] ['inspector'] = isset ( $inspector [$v ['inspector_id']] ) ? $inspector [$v ['inspector_id']] : array ();
			}
		}
		return $res;
	}
	function getSubByKey($pArray, $pKey = "", $pCondition = "") {
		$result = array ();
		foreach ( $pArray as $temp_array ) {
			if (is_object ( $temp_array )) {
				$temp_array = ( array ) $temp_array;
			}
			if (("" != $pCondition && $temp_array [$pCondition [0]] == $pCondition [1]) || "" == $pCondition) {
				$result [] = ("" == $pKey) ? $temp_array : isset ( $temp_array [$pKey] ) ? $temp_array [$pKey] : "";
			}
		}
		return $result;
	}
}